package com.a3g.findnumber.entity;

import android.util.Log;

import com.a3g.findnumber.GamePlayAct;

public class ThreadTime extends Thread {

	private static ThreadTime threadTime;

	private GamePlayAct playActivity;

	public static int SLEEP_TIME = 200;

	public static int MAX_TIME_LV_1 = 1800;

	public static int MAX_TIME_LV_2 = 1600;

	public static int MAX_TIME_LV_3 = 1400;

	public static int MAX_TIME_LV_4 = 1200;

	public static int MAX_TIME_LV_5 = 1000;

	private int timmer;

	public ThreadTime(GamePlayAct playActivity, int level) {
		switch (level) {
			case 1:
				this.timmer = ThreadTime.MAX_TIME_LV_1;
				break;
			case 2:
				this.timmer = ThreadTime.MAX_TIME_LV_2;
				break;
			case 3:
				this.timmer = ThreadTime.MAX_TIME_LV_3;
				break;
			case 4:
				this.timmer = ThreadTime.MAX_TIME_LV_4;
				break;
			case 5:
				this.timmer = ThreadTime.MAX_TIME_LV_5;
				break;
		}

		this.playActivity = playActivity;
	}

	public static void createNewThreadTime(GamePlayAct playActivity, int level) {

		ThreadTime.destroyThreadTime();
		ThreadTime.threadTime = new ThreadTime(playActivity, level);
		ThreadTime.threadTime.start();
	}

	public static void destroyThreadTime() {
		if (ThreadTime.threadTime != null) {
			Log.e("ThreadTime", "Deleted a Thread");
			ThreadTime.threadTime.interrupt();
		}
	}

	public static int getLevelDuration(int level) {
		int result;

		switch (level) {
			case 1:
				result = ThreadTime.MAX_TIME_LV_1;
				break;
			case 2:
				result = ThreadTime.MAX_TIME_LV_2;
				break;
			case 3:
				result = ThreadTime.MAX_TIME_LV_3;
				break;
			case 4:
				result = ThreadTime.MAX_TIME_LV_4;
				break;
			case 5:
				result = ThreadTime.MAX_TIME_LV_5;
				break;
			default:
				result = 0;
				break;
		}

		return result;
	}

	@Override
	public synchronized void start() {
		super.start();
	}

	public synchronized void destroy() {

	}

	@Override
	public synchronized void run() {
		super.run();
		try {
			while (this.timmer >= 0) {
				sleep(ThreadTime.SLEEP_TIME);
				this.playActivity.setTime(this.timmer);
				this.timmer = this.timmer - ThreadTime.SLEEP_TIME;
			}

		} catch (InterruptedException e) {
			// Log.e("Thread Time: ", e.toString());
		}
	}

}
